10 map
<int, set
<string
> > dict
;
11 map
<char, char> solucion
;
14 bool contradice(const string
&origen
, const string
&destino
, map
<char, char> crypt
){
15 //cout << "Contradice llamado con " << origen << " y " << destino << endl;
18 for (int i
=0; i
<origen
.size(); ++i
){
19 if (temp
.count(origen
[i
]) == 1){
20 if (temp
[origen
[i
]] != destino
[i
]){
24 for (map
<char, char>::iterator j
= temp
.begin(); j
!= temp
.end(); ++j
){
25 if ((*j
).second
== destino
[i
]) return true;
27 temp
[origen
[i
]] = destino
[i
];
31 if (origen
.length() != destino
.length()) return true;
33 for (int i
=0; i
<origen
.size(); ++i
){
34 if (crypt
.count(origen
[i
]) == 1){
35 if (crypt
[origen
[i
]] != destino
[i
]){
39 for (map
<char, char>::iterator j
= crypt
.begin(); j
!= crypt
.end(); ++j
){
40 if ((*j
).second
== destino
[i
]) return true;
49 void asignar(const string
&origen
, const string
&destino
, map
<char, char> &crypt
){
50 if (origen
.size() != destino
.size()) return;
52 for (int i
=0; i
<origen
.size(); ++i
){
53 crypt
[origen
[i
]] = destino
[i
];
57 bool bt(int i
, const vector
<string
> &words
, map
<char, char> crypt
){
58 if (i
>= words
.size()){
63 int len
= words
[i
].length();
64 set
<string
> possible
= dict
[len
];
66 for (set
<string
>::iterator j
= possible
.begin(); j
!= possible
.end(); ++j
){
68 if (!contradice(words
[i
], (*j
), crypt
)){
69 map
<char, char> copia
= crypt
;
70 asignar(words
[i
], (*j
), copia
);
72 if (bt(i
+1, words
, copia
)){
87 dict
[s
.length()].insert(s
);
88 //cout << "agregue " << s << " al dict en la posicion " << s.length() << endl;
92 while (getline(cin
, line
)){
93 //cout << "Lei esta linea: " << line << endl;
100 vector
<string
> words
;
105 words
.push_back(word
);
107 //cout << "Empuje la palabra " << word << " a la lista " << endl;
110 if (bt(0, words
, map
<char, char>() )){
111 for (int i
=0; i
<line
.size(); ++i
){
112 if (line
[i
] == ' ') cout
<< " ";
113 else cout
<< solucion
[line
[i
]];
116 //cout << "Hay solucion\n";
118 for (int i
=0; i
<line
.size(); ++i
){
119 if (line
[i
] == ' ') cout
<< " ";
123 //cout << "No hay solucion\n";